/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import manager.ServicePlanManager;
import manager.StakeholderManager;
import manager.TransactionsManager;
import session.stateless.ServicePlanSessionLocal;
import session.stateless.StakeholderSessionLocal;
import session.stateless.TransactionsSessionLocal;

/**
 *
 * @author Jacko
 */
@WebServlet(name = "Controller", urlPatterns = {"/Controller", "/Controller?*"})
public class Controller extends HttpServlet {

    @EJB
    ServicePlanSessionLocal servicePlanSessionLocal;
    @EJB
    StakeholderSessionLocal stakeholderSessionLocal;
    @EJB
    TransactionsSessionLocal transactionsSessionLocal;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();


        try {
            StakeholderManager stakeholderManager = new StakeholderManager(stakeholderSessionLocal);
            ServicePlanManager servicePlanManager = new ServicePlanManager(servicePlanSessionLocal);
            TransactionsManager transactionsManager = new TransactionsManager(transactionsSessionLocal);

            String action = request.getParameter("action");

            if (action == null) {
                action = "index";
            }

            if (action.equals("index")) {
                request.getRequestDispatcher("/index.jsp").forward(request, response);
            } 
            else if (action.equals("doLogin")) {
                Long stakeholderID = stakeholderManager.doLogin(request.getParameter("username"), request.getParameter("password"));

                if (stakeholderID == null) {
                    request.getSession().setAttribute("invalidLogin", "true");
                    request.getRequestDispatcher("/login.jsp").forward(request, response);
                } 
                else {
                    request.getSession().setAttribute("stakeholderID", stakeholderID);
                    request.getRequestDispatcher("/Controller?action=" + request.getSession().getAttribute("lastAction")).forward(request, response);
                }
            } 
            else {
                if (request.getSession().getAttribute("stakeholderID") == null) {
                    request.setAttribute("invalidLogin", "false");
                    request.getSession().setAttribute("lastAction", action);
                    request.getRequestDispatcher("/login.jsp").forward(request, response);

                } 
                else {
                    if (action.equals("doLogout")) {
                        request.getSession().setAttribute("stakeholderID", null);
                        request.getRequestDispatcher("/index.jsp").forward(request, response);
                    } else if (action.equals("viewAllServicePlan")) {
                        request.setAttribute("servicePlan", servicePlanManager.getAllServicePlan());
                        request.getRequestDispatcher("/viewAllServicePlan.jsp").forward(request, response);
                    } else if (action.equals("modifyServicePlan")) {
                        request.setAttribute("servicePlan", servicePlanManager.modifyServicePlan());
                        request.getRequestDispatcher("/modifyServicePlan.jsp").forward(request, response);
                    }else if (action.equals("addServicePlan")) {
                        request.setAttribute("newSvcPlanID", new Long(0));
                        request.getRequestDispatcher("/addServicePlan.jsp").forward(request, response);
                    } else if (action.equals("saveServicePlan")) {
                        try {
                            request.setAttribute("newSvcPlanID", servicePlanManager.addServicePlan(request));
                            request.setAttribute("status", "success");
                            System.out.println("******** SERVICE PLAN ID: " + request.getAttribute("newSvcPlanID").toString());
                        } catch (Exception ex) {
                             ex.printStackTrace();
                        }// Need to load venues again
                        //request.setAttribute("venues", eventManager.getAllVenues());
                        request.getRequestDispatcher("/addServicePlan.jsp").forward(request, response);
                    } 

                    else if(action.equals("viewAllTransactions")) {
                        request.setAttribute("transactions", transactionsManager.getAllTransactions());
                        request.getRequestDispatcher("/viewAllTransactions.jsp").forward(request, response);
                    } 
                    else if (action.equals("addTransaction")) {
                        request.setAttribute("svcPlanID", new Long(0));
                        request.getRequestDispatcher("/addServicePlan.jsp").forward(request, response);
                    } 
                    else if (action.equals("saveTransaction")) {
                        try {
                            request.setAttribute("transID", transactionsManager.addTransaction(request));
                            request.setAttribute("status", "success");
                            System.out.println("******** TRANSACTION ID: " + request.getAttribute("transID").toString());
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                        request.getSession().setAttribute("lastAction", action);
                        request.getRequestDispatcher("/login.jsp").forward(request, response);
                    }

                }
            }
        } 
        finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
